﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.Blo;

namespace UI
{
    public class AcCollectionWarriorItem : MonoBehaviour
    {
        public UILabel labTitle;
        public UILabel labLeftTimes;
        public UILabel leftLabel;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public UISprite titleLogo;

        public GameObject btnGet;
        public GameObject imgGetted;
        public GameObject imgNotReached;
        public GameObject commonIcon;
		public GameObject heroIcon;
        public delegate void OnGetRewardCallback(List<ActivityItem> list);
        public OnGetRewardCallback m_callback;

        public delegate void AddRedNum();
        public AddRedNum m_addNum;

        List<BaseResource> getRes;
        string activityId;
        int itemIndex;
        int oneOrAll = 2;
        public ActivityType type;
        public void SetActivityItem(ActivityItem activityItem, string acId)
        {

            if (activityItem.index == 1)
                titleLogo.spriteName = "UI_item_title_xing_blue";
            else if (activityItem.index == 2)
                titleLogo.spriteName = "UI_item_title_xing_zi";
            else
                titleLogo.spriteName = "UI_item_title_xing_green";

            labTitle.text = "[000000]"+activityItem.message+"[-]";
            labLeftTimes.text = activityItem.left.ToString() + "/" + activityItem.total.ToString();
            leftLabel.text = TableTextBlo.GetText("schedule") + ": ";
            activityId = acId;
            itemIndex = activityItem.index;

            //GameObject heroObj = Instantiate(heroIcon);
            heroIcon.GetComponent<CommonIcon>().SetData(activityItem.target, true, -1);


            //奖励
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();

            float scale = 0.9f;
            for (int i = 0; i < activityItem.rewardList.Count; i++)
            {

                //奖励内容
                GameObject obj = Instantiate(commonIcon);
                obj.SetActive(true);
                simpleGrid.AddChild(obj);
                //obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = new Vector3(scale, scale, scale);
                obj.GetComponent<CommonIcon>().SetData(activityItem.rewardList[i].id, true, activityItem.rewardList[i].num);
            }
            getRes = activityItem.rewardList;
            oneOrAll = activityItem.pickOneOrGetAll;
            imgNotReached.SetActive(activityItem.status == 0);
            btnGet.SetActive(activityItem.status == 1);
            imgGetted.SetActive(activityItem.status == 2);
            if (activityItem.status == 1 && m_addNum != null)
                m_addNum();
        }

      
        public void OnBtnGet()
        {
            
            if (oneOrAll == 1) {
                CSActivityReward msg = new CSActivityReward();
                msg.activityId = this.activityId;
                msg.index = itemIndex;
                msg.getResources.AddRange(getRes);
                NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
            }
            else if (oneOrAll == 0)
            {
                PageManager.Instance.OpenPage("SelectAcRewardPage", "");
                SelectAcRewardPage reward = PageManager.Instance.GetDictPage("SelectAcRewardPage") as SelectAcRewardPage;
                reward.m_click = this.SelectAcAward;
                reward.SetReward(getRes);
            }
        }

        void SelectAcAward(BaseResource item)
        {
            CSActivityReward msg = new CSActivityReward();
            msg.activityId = this.activityId;
            msg.index = itemIndex;
            for (int i = 0; i < getRes.Count; i++)
            {
                if (getRes[i].id == item.id)
                {
                    msg.getResources.Add(getRes[i]);
                    break;
                }
            }
            NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
        }

        void GetRewardCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityReward msg = packet.kBody as SCActivityReward;
                StaticData.playerData.AddResultObj(msg.get);

                CommonMethod.ShowGetObjs(msg.get);

                if (m_callback != null)
                {
                    m_callback(msg.itemList);
                }
                StaticData.redTips.SetPointState(RedPointTip.Red.warriorGetSpecial, 0);
                StaticData.redTips.ReduceActivitys();
            }
        }
    }
}

